#!/user/bin/python
#coding=utf-8
from time import sleep
import re,os,sys,serial,test,common 
execfile("../cfg/at/other.cfg")
net_card = ""

def cmd_send_ping(command):
	out_str = os.popen(command).read()
	out_result = re.findall("([0-9]+) received, ([0-9]+)",out_str)
	if len(out_result) != 0 :
		test.rndis_ping_time = test.rndis_ping_time + 5
		test.rndis_ping_suc = test.rndis_ping_suc + int(out_result[0][0])
		test.rndis_ping_fail = test.rndis_ping_fail + 5 - int(out_result[0][0])
		print "发送5个包, 收到"+out_result[0][0]+"个包, "+"失败率是："+out_result[0][1]
		if out_result[0][1] != "0":
			print(out_str)

def start_net(result_ob):
	print '打开模块对应的网卡'
	global net_card
	result_ob.add_test_time(1)
	print '打开网卡'+net_card+'成功'
	os.system("ifconfig "+net_card+" up")
	result_ob.add_suc_time(1)
	sleep(4)
'''
	else:
		cmd=os.popen("ifconfig -a").read()
		#print result
		result = re.findall("(eth[0-9]+)",cmd)
		if not result:
			result_ob.add_fail_time(1)
			print("请查看网卡是否有问题.....")
			test.output_result()
			sys.exit(2)
		if len(result)<2:
			result_ob.add_fail_time(1)
			print("请查看网卡是否有问题.....")
			test.output_result()
			sys.exit(2)
		for i in range(len(result)):
			if result[i]!= 'eth0':
				result_ob.add_suc_time(1)
				net_card = result[i]
				os.system("ifconfig "+net_card+" up")
				print '打开网卡'+net_card+'成功'
				return
		if not net_card:
			print '请查看网卡是否有问题.....'
			result_ob.add_fail_time(1)
			test.output_result()
			sys.exit(2)
'''
def close_net():
	print '关闭模块对应的网卡'
	global net_card
	os.system('ifconfig '+net_card+' down')
	sleep(5)

def run(result_object):
	#port = sys.argv[1]
	ser = ""
	global net_card
#	log_file = open("message.log","a")
#	sys.stdout = log_file
	for i in range(test_netCard_loop): 
			ser = common.ser_is_ok(dev_port)
			#判断是否联网成功
			if common.net_is_ok(ser):
				net_card = common.set_all_net()
				if net_card=="":
					print '找不到网卡'
					result_object.add_fail_time(1)
					test.output_result()
					sys.exit(2)

				#ping测试
				common.ping_test(result_object,"ping -c "+str(rndis_ping_loop)+" www.baidu.com"+" -I "+net_card,rndis_ping_loop)                	
				common.ping_test(result_object,"ping -c "+str(rndis_ping_loop)+" www.youku.com"+" -I "+net_card,rndis_ping_loop)		
				#断开网卡
				ser.close()
